$(function () {
    let arr =[];
    let weight;
    //*************************************************
    onEv();
    initPage();// 初始化页面 --ok
    setChangePage( getItemList );// 页码切换 --ok

    // 初始化页面 --ok
    function initPage() {
        let itemType_header = [],
            itemType_son = [];

        let getCollege = new Promise( ( resolve, reject)=>{
            $.ajax({
                url : '/institute/list.do',
                type : 'post',
                data : {},
                dataType : 'json',
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success : function (data){
                    if(data.status == 0){
                        resolve(data);
                    }else if(data.status == 10){
                        reject(data);
                    }else{
                        reject(data);
                    }
                },
                error : function (data){
                    reject(data);
                }
            })
        } );
        let getItemType = new Promise( (resolve, reject)=> {
            // 获取所有申报类型
            $.ajax({
                url : '/manage/racetype/get_raceType.do',
                type:'post',
                data : {parentId:0},
                dataType : 'json',
                ContentType : 'application/x-www-form-urlencoded',
                withCredential : true ,
                xhrFields : {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if(data.status == 0){
                        resolve( data );
                    }else if(data.status ==10){
                        reject(data);
                    }else{
                        reject(data);
                    }
                },
                error : function(data){
                    reject(data);
                }
            });
        } );
        let initSelect = Promise.all( [getCollege, getItemType] );

        initSelect.then( (data)=>{
            // let college = data[0].data;// 学院
            itemType_header = data[1].data;// itemType

            // // 学院
            // let college_Box = $('#college-Sele');
            // college_Box.html('');
            // college_Box.prepend($('<option value="">全部学院</option>'));
            // for( let i=0; i<college.length; i++ ){
            //     let DOM = $('<option value="'+ college[i].stuinstitutename +'">'+ college[i].stuinstitutename +'</option>');
            //     college_Box.append( DOM );
            // }

            // item类型
            getSonType( itemType_header );
        }).catch((data)=>{
            if(data.status == 10){
                spop_er(data.msg,function () {},outLogin);
            }
        });

        // 获取子项目类型 --ok
        function getSonType( itemType_header ) {
            console.log('zzzzzzzzzzzzzzzzz');
            for( let i=0; i<itemType_header.length; i++ ){
                // 获取每个子项目类型
                itemType_son[i] = new Promise((resolve, reject) => {
                    $.ajax({
                        url : '/manage/racetype/get_raceType.do',
                        type:'post',
                        data : {
                            parentId : itemType_header[i].racetypeno
                        },
                        dataType : 'json',
                        ContentType : 'application/x-www-form-urlencoded',
                        withCredential : true ,
                        xhrFields : {
                            withCredentials: true
                        },
                        crossDomain: true,
                        success: function (data) {
                            if(data.status == 0){
                                resolve( data );
                            }else if( data.status == 10 ){
                                reject( data );
                            }
                        },
                        error : function (data) {
                            reject( data );
                        }
                    });
                });
            }

            let itemSon = Promise.all( itemType_son );

            itemSon.then( (data)=>{
                To_ItemType(data);
            }).catch( (data)=>{
                if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }
            } );
        }
        // 渲染子项目类型 ==> 获取项目列表 --ok
        function To_ItemType(data) {
            console.log(data);
            let projecttype_Box = $('#projecttype-Sele');
            projecttype_Box.html('');
            projecttype_Box.append( $('<option value="">全部项目类型</option>') );

            // 头
            for(let i=0; i<itemType_header.length; i++){
                let selectDOM = $('<optgroup label="'+ itemType_header[i].racetype +'"></optgroup>');

                // son
                for(let j=0; j<data[i].data.length; j++){
                    let info = data[i].data;
                    let optionDOM = $('<option value="'+ info[j].racetypeno +'">'+ info[j].racetype +'</option>');
                    selectDOM.append( optionDOM );
                }
                projecttype_Box.append( selectDOM );
            }

            // 获取项目列表
            getItemList();
        }
    }

    // 获取项目列表 -ok
    function getItemList() {
        $.ajax({
            url: '/manage/projectDetails/list_product.do',
            type: 'post',
            data: {
                pageNum : oPageInfo.pageNum,
                pageSize : oPageInfo.pageSize,
                name : oPageInfo.searchInfo,
                startTime : oPageInfo.startTime,
                stopTime : oPageInfo.stopTime,

                projectType : oPageInfo.projectType,
                stuInstitute : oPageInfo.stuInstitute,
                examineStatus : oPageInfo.examineStatus
            },
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.status == 0) {
                    // 渲染项目列表
                    let itemObj = data.data;
                    To_ItemList( itemObj    ) ;
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.msg );
                }
            }
        });
    }
    // 渲染页面 --ok
    function To_ItemList( oData ){
        let tabelBox = $('#itemTable').find('tbody');
        tabelBox.html('');

        let aData = oData.list;
        aData.forEach( (item,idx)=>{
            // let DOm = '';
            // tabelBox.append( DOM );
        } );

        oPageInfo.pages = oData.pages;
        oPageInfo.pageNum = oData.pageNum;
        oPageInfo.pageSize = oData.pageSize;
        oPageInfo.pageTotal = oData.pageTotal;

        setPageFooter();
    }

    // 绑定事件
    function onEv() {
        $('#projecttype-Sele').on('change', onLookNum );
        $('#subSearch').on('click', subSearch);
        $('#rater').on('input', countNum );
        $('#vote').on('input', countNum);
        $('#export').on('click', exportFn);
        $('#examineStatus-Sele').on('change', function () {
            for(let i=0; i<arr.length; i++){
                if ( $(this).val() == arr[i].racetypesamsarano ){
                    weight = arr[i].weight ? arr[i].weight : 50 ;
                    $('#rater').val( weight );
                    $('#vote').val(100-weight);
                    return;
                }
            }
        })
    }
    function sb() {
    }
    // 轮次监听
    function onLookNum() {
        $.ajax({
            url: '/manage/racetypesamsara/list_racetype_samsara.do',
            type: 'post',
            data: {
                racetypeno : $(this).val()
            },
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.status == 0) {       	
                    renderLookNum( data.data );
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.msg );
                }
            }
        });
        
    }
    function renderLookNum( list ) {

        arr = list;
        let i=0;
        $('#examineStatus-Sele').html('');
        for( let item of list  ){

        	if(i==0){
        	    i=i+1;
        		$('#rater').val(item.weight);
                $('#vote').val(100-item.weight);
                if(item.weight==null){
                    $('#rater').val(0);
                }
        	}
            $('#examineStatus-Sele').append( `<option value="${item.racetypesamsarano}" data-wight="${item.weight}">${item.racetypesamsaraname}</option>` )
        }
    }
    function countNum() {
        let type = $(this).attr('name');
         let dom = type=='rater'? $('#vote') : $('#rater');
        dom.val( eval( 100 - $(this).val() ) );
    }
    //
    function subSearch() {
        $.ajax({
            url: '/manage/projectDetails/list_product_result.do',
            type: 'post',
            data: {
                racetypesamsarano : $('#examineStatus-Sele').val(),
                weightFrom: $('#rater').val()
            },
            dataType: 'json',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.status == 0) {
                    renderLIts( data.data );
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin )
                }else{
                    spop_er( data.msg );
                }
            }
        });
    }
    function renderLIts( oData ) {
        let tabelBox = $('#itemTable').find('tbody');
        tabelBox.html('');

        let aData = oData.list;
        aData.forEach( (item,idx)=>{

            let DOM = $(`<tr>
                <td>${item.projectname}</td>
                <td>${item.instructor}</td>
                <td>${item.projectgrade}</td>
                <td>${item.projecttype}</td>
                <td>${item.stuInstitute}</td>
                <td>${item.reviewnotes}</td>
                <td>${item.ballot}</td>
                <td>${item.aggregateScore}</td>
                </tr>`);
            tabelBox.append( DOM );
        } );

        oPageInfo.pages = oData.pages;
        oPageInfo.pageNum = oData.pageNum;
        oPageInfo.pageSize = oData.pageSize;
        oPageInfo.pageTotal = oData.pageTotal;

        setPageFooter();
    }
    // 
    function exportFn() {
        if( $('#examineStatus-Sele').val()=='' || $('#rater').val()=='' ){
            return spop_waring('请先筛选数据后操作');
        }
        window.open( `/manage/projectDetails/result_export_busInfo.do?racetypesamsarano=${$('#examineStatus-Sele').val()}&weightFrom=${$('#rater').val()}` )

    }
});